package com.xmut.bs.handaccount.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtils {

    /**
     * 调用此方法输入所要转换的时间戳输入例如（1402733340）输出（"2014-06-14  16:09:00"）
     */
    public static String stampToStringWithSprit(int time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        return sdr.format(new Date(time * 1000L));
    }

    public static String stampToStringWithSprit(long time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        return sdr.format(new Date(time * 1000L));
    }

    public static String stampToStringWithSprit(long time, String pattern) {
        SimpleDateFormat sdr = new SimpleDateFormat(pattern, Locale.getDefault());
        return sdr.format(new Date(time * 1000L));
    }

    public static String stampToStringWithSprit(String time, String pattern) {
        SimpleDateFormat sdr = new SimpleDateFormat(pattern, Locale.getDefault());
        return sdr.format(new Date(Long.parseLong(time) * 1000L));
    }

    public static String stampToStringWithSprit(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
        return sdr.format(new Date(Long.parseLong(time) * 1000L));
    }

    public static String StringTostampWithSprit(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        try {
            Date date = sdr.parse(time);
            return String.valueOf(date.getTime() / 1000L);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static Date StringToDateWithSprit(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        try {
            return sdr.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String DateToString(Date time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
        return sdr.format(time);
    }

    public static long StringToLongWithSprit(String time) {
        SimpleDateFormat sdr = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault());
        try {
            Date date = sdr.parse(time);
            return date.getTime() / 1000L;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return 0L;
    }

    /**
     * 获取当前时间的年月日时分秒
     *
     * @return
     */
    public static String current() {
        Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH) + 1;
        int day = c.get(Calendar.DAY_OF_MONTH);
        int hour = c.get(Calendar.HOUR_OF_DAY);
        int minute = c.get(Calendar.MINUTE);
        int second = c.get(Calendar.SECOND);
        return year + "-" + month + "-" + day + " " + hour + ":" + minute + ":" + second + ":";
    }

}


